package cn.doctor.assistant.pad.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.doctor.assistant.pad.R;

/**
 * Created by jiaolf on 2016/3/14.
 */
public class NavigationAdapter extends BaseAdapter {

    private List<MainItem> mainItems;
    private Context mContext;

    public NavigationAdapter(Context context, List<MainItem> data) {
        mContext = context;
        mainItems = data;
    }

    View viewMsgTip;
    public void setChatMessageState(boolean hasNewMessage) {
        // index 4 是互动聊天
        //getItem(4).setShowTip(hasNewMessage);
        if (viewMsgTip != null) {
            viewMsgTip.setVisibility(hasNewMessage ? View.VISIBLE : View.GONE);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mainItems.size();
    }

    @Override
    public MainItem getItem(int position) {
        return mainItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            boolean isChat = getItem(position).getLabel().equals("互动");
            if (isChat) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.main_item_chat, parent, false);
                viewMsgTip = convertView.findViewById(R.id.main_item_unread_msg_tip);
            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.main_item, parent, false);
            }

            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.main_item_icon);
            viewHolder.label = (TextView) convertView.findViewById(R.id.main_item_lable);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.icon.setImageResource(getItem(position).getImgId());
        viewHolder.label.setText(getItem(position).getLabel());

        return convertView;
    }

    private class ViewHolder {
        private ImageView icon;
        private TextView label;
    }
}
